//-----------------------------------------------------------------------
//<copyright file="PhieuGiaoHangDAO.cs
//Code generate by tools.
//</copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using PMS.Exceptions;
using System.Data.SqlClient;
using PMS.Entity;

namespace PMS.DAL
{
	public class PhieuGiaoHangDAO : SQLDatabase, IPhieuGiaoHangDAO
	{

		#region Create PhieuGiaoHang methods

		public PhieuGiaoHang CreatePhieuGiaoHang(PhieuGiaoHang phieuGiaoHang)
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuGiaoHangInsert");
				AddOutParameter(command, "@SoPG", SqlDbType.NVarChar, phieuGiaoHang.SoPG);
				AddInParameter(command, "@NgayGiao", SqlDbType.DateTime, phieuGiaoHang.NgayGiao);
				AddInParameter(command, "@PhuongThucGiao", SqlDbType.NVarChar, phieuGiaoHang.PhuongThucGiao);
				AddInParameter(command, "@PhuongThucTT", SqlDbType.NVarChar, phieuGiaoHang.PhuongThucTT);
				AddInParameter(command, "@NguoiGiao", SqlDbType.NVarChar, phieuGiaoHang.NguoiGiao);
				AddInParameter(command, "@NguoiNhan", SqlDbType.NVarChar, phieuGiaoHang.NguoiNhan);
				AddInParameter(command, "@SoHD", SqlDbType.NVarChar, phieuGiaoHang.SoHD);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, phieuGiaoHang.GhiChu);
				AddInParameter(command, "@MaKH", SqlDbType.NVarChar, phieuGiaoHang.MaKH);
				AddInParameter(command, "@SoPX", SqlDbType.NVarChar, phieuGiaoHang.SoPX);

				ExecuteNonQuery(command);
                phieuGiaoHang.SoPG = (string)GetParameterValue(command, "@SoPG");
				return phieuGiaoHang;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access CreatePhieuGiaoHangException", ex);
			}
		}

		#endregion

		#region Update PhieuGiaoHang methods

		public void UpdatePhieuGiaoHang(PhieuGiaoHang phieuGiaoHang)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuGiaoHangUpdate");

				AddInParameter(command, "@SoPG", SqlDbType.NVarChar, phieuGiaoHang.SoPG);
				AddInParameter(command, "@NgayGiao", SqlDbType.DateTime, phieuGiaoHang.NgayGiao);
				AddInParameter(command, "@PhuongThucGiao", SqlDbType.NVarChar, phieuGiaoHang.PhuongThucGiao);
				AddInParameter(command, "@PhuongThucTT", SqlDbType.NVarChar, phieuGiaoHang.PhuongThucTT);
				AddInParameter(command, "@NguoiGiao", SqlDbType.NVarChar, phieuGiaoHang.NguoiGiao);
				AddInParameter(command, "@NguoiNhan", SqlDbType.NVarChar, phieuGiaoHang.NguoiNhan);
				AddInParameter(command, "@SoHD", SqlDbType.NVarChar, phieuGiaoHang.SoHD);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, phieuGiaoHang.GhiChu);
				AddInParameter(command, "@MaKH", SqlDbType.NVarChar, phieuGiaoHang.MaKH);
				AddInParameter(command, "@SoPX", SqlDbType.NVarChar, phieuGiaoHang.SoPX);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access UpdatePhieuGiaoHangException", ex);
			}
		}

		#endregion

		#region Delete PhieuGiaoHang methods

		public void DeletePhieuGiaoHang(PhieuGiaoHang phieuGiaoHang)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuGiaoHangDelete");

				AddInParameter(command, "@SoPG", SqlDbType.NVarChar, phieuGiaoHang.SoPG);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access DeletePhieuGiaoHangException", ex);
			}
		}

		#endregion

		#region Select methods

		#region GetAll PhieuGiaoHang methods

		public PhieuGiaoHangs GetAllPhieuGiaoHang()
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuGiaoHangGetList");
				PhieuGiaoHangs Result = new PhieuGiaoHangs();
				List<PhieuGiaoHang> List = ExecuteQuery<PhieuGiaoHang>(command,true);
				foreach(PhieuGiaoHang item in List)
				{
					Result.Add(item);
				}
				return Result;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetAllPhieuGiaoHangException", ex);
			}
		}

		#endregion


		#region GetPhieuGiaoHang methods

		public PhieuGiaoHang GetPhieuGiaoHangByID(string soPG)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuGiaoHangGet");

				AddInParameter(command, "@soPG", SqlDbType.NVarChar, soPG);

				return ExecuteQuery<PhieuGiaoHang>(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetPhieuGiaoHangBy ID Exception", ex);
			}
		}

        public PhieuGiaoHang GetPhieuGiaoHangBySoPX(string soPX)
        {
            try
            {
                SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuGiaoHangSelectAllBySoPX");

                AddInParameter(command, "@soPX", SqlDbType.NVarChar, soPX);

                return ExecuteQuery<PhieuGiaoHang>(command);
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access GetPhieuGiaoHangBy ID Exception", ex);
            }
        }

		#endregion

        #region GetPhieuGiaoHangByDate methods

        public PhieuGiaoHangs GetPhieuGiaoHangByDate(DateTime? tungay, DateTime? denngay)
        {
            try
            {

                SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuGiaoHangGetList");
                AddInParameter(command, "@tungay", SqlDbType.DateTime, tungay);
                AddInParameter(command, "@denngay", SqlDbType.DateTime, denngay);
                PhieuGiaoHangs Result = new PhieuGiaoHangs();
                List<PhieuGiaoHang> List = ExecuteQuery<PhieuGiaoHang>(command, true);
                foreach (PhieuGiaoHang item in List)
                {
                    Result.Add(item);
                }
                return Result;
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access GetAllPhieuGiaoHangException", ex);
            }
        }

        #endregion

		#endregion
	}
}//End Class
